package com.jy.web.file.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.jy.framework.exception.MyException;
import com.jy.framework.utils.os.OSProperties;
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.InternalThreadLocalMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: JuiceYan
 * @Description: 本地文件处理
 * @Class: LocalFileServiceImpl
 * @Date: 9:37 2018/11/5
 */
@Service("localFileService")
@Slf4j
public class File2Url {

    public static final String URL_SEPARATOR = "/";
    public static final String HTTP_URL = "HTTP_URL";
    public static final String REAL_PATH = "REAL_PATH";


    private static String businessType;

    private static OSProperties osProperties;
    @Autowired
    public File2Url(OSProperties osProperties,@Value("${jy.businessType}")String businessType ) {
        File2Url.osProperties = osProperties;
        File2Url.businessType = businessType;

    }

//    private static FastThreadLocal<Map<String,String>> fileUrl = new FastThreadLocal<>();

    public static String transferFile2Url(MultipartFile file, String businessType) throws Exception {
        if (file.isEmpty()){
            throw new MyException("请先选择需上传的文件！");
        }
        File file1 = new File(file.getOriginalFilename());
        String name = file1.getName();
        String subFolder = name.split("\\.")[1].toLowerCase();
        String url=subFolder;
        if (StrUtil.isNotBlank(businessType)){
            subFolder=businessType+File.separator+subFolder;
            url=businessType+URL_SEPARATOR+url;
        }
        String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
        long nanoTime = System.nanoTime();
        url += URL_SEPARATOR + yyyyMMdd+URL_SEPARATOR+nanoTime + URL_SEPARATOR;
        subFolder += File.separator +yyyyMMdd+File.separator+ nanoTime + File.separator;
        String realPath= osProperties.getFilesPath() + subFolder;
        File file2 = new File(realPath);
        file2.mkdirs();
        file.transferTo(new File(realPath+name));
        String path = osProperties.getFilesUrl()+url+name;
        return  osProperties.getServerUrl()+ path;
//        Map<String,String> pathUrl = new HashMap<>();
//        pathUrl.put(REAL_PATH,realPath+name);
//        pathUrl.put(HTTP_URL,path);
//        fileUrl.set(pathUrl);
    }
    public static String transferFile2Url(MultipartFile file) throws Exception {
        return File2Url.transferFile2Url(file, businessType);
    }

    public static String transferFile2Url(File file1, String businessType) throws Exception {
        String name = file1.getName();
        String subFolder = name.split("\\.")[1].toLowerCase();
        String url=subFolder;
        if (StrUtil.isNotBlank(businessType)){
            subFolder=businessType+File.separator+subFolder;
            url=businessType+URL_SEPARATOR+url;
        }
        String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
        long nanoTime = System.nanoTime();
        url += URL_SEPARATOR + yyyyMMdd+URL_SEPARATOR+nanoTime + URL_SEPARATOR;
        subFolder += File.separator +yyyyMMdd+File.separator+ nanoTime + File.separator;
        String realPath= osProperties.getFilesPath() + subFolder;
        File file2 = new File(realPath);
        file2.mkdirs();
        FileUtil.copyFile(file1,file2);
        String path = osProperties.getFilesUrl()+url+name;
        return  osProperties.getServerUrl()+ path;
    }


    public static String httpUrl2RealFilePath(String path){
        return  path.replace(osProperties.getFilesUrl(), "").replace(osProperties.getServerUrl(), osProperties.getFilesPath());
    }


   }
